package sicnu.cs.ich.log.canal;

import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import sicnu.cs.ich.common.services.impl.RedisService;

import java.util.function.Function;

/**
 * @author CaiKe
 * @date 2021/12/10 16:05
 */
@Component
public class BaseHandler<T> {

    @Autowired
    protected RedisService redisService;
    @Autowired
    protected DefaultMQProducer producer;

    @SneakyThrows
    protected void deleteCache(Function<T, String> function, T t) {
        String key = function.apply(t);
        if (!redisService.removeValue(key)) {
            final Message message = new Message("redis", "del", JSON.toJSONBytes(key));
            // 设置第一层延迟时间，1s
            message.setDelayTimeLevel(1);
            producer.send(message);
        }
    }
}